<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML>
<html lang="en">
<head>
<base href="<%=basePath%>">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/main.css">
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/main.js"></script>
<title>商城管理系统</title>
<style>
.main_div {
	min-width: 1000px;
}

.pageChange {
	float: right;
	color: #337ab7;
}

.pageChange>span {
	cursor: pointer
}

.pageChange>span:hover {
	color: blue
}
</style>
</head>
<body>
	<%@ include file="header.jsp"%>
	<%@ include file="left.jsp"%>
	<div id="index"
		style="position: absolute;left: 180px;top: 80px;bottom: 0;right: 0;">
		<section id="main">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div" style="height:40px">
					<div class="main_title">
						<span>商品管理</span>
					</div>
					<form class="form-inline"
						style="margin-top: 40px;margin-left: 20px">
						<div class="form-group">
							<label>商品名称:</label> <input class="form-control"
								id="productNames">
						</div>
					<!--  	<label>商品类型</label> <select class="form-control" id="types"
							name="type">
							<option value="-1">请选择</option>
							<option value="0">一般商品</option>
							<option value="1">首页商品</option>
						</select>-->
						<!--  <label>商家名称</label> <select class="form-control" id="directId">
							<option value="-1">请选择</option>
						</select>-->
						<button type="button" onclick="selectinfo()"
							class="btn btn-primary">立即查询</button>
						<input style="display:none" value="5" id="pageCount"
							name="pageCount" />
						<button style="margin-left:20%" type="button"
							class="btn btn-primary" onclick="detailinfos()">添加商品</button>
					</form>
				</div>
				<div class="force-overflow1-1">
					<table id="showtable"
						class="table table-condensed table-bordered table-hover"
						style="text-align: center">
						<tr style="background-color: #f8f8f8">
							<th style="text-align: center;color: #000">序号</th>
							<th style="text-align: center;color: #000">商品广告图</th>
							<th style="text-align: center;color: #000">商品名称</th>
							<th style="text-align: center;color: #000">商品原价</th>
							<th style="text-align: center;color: #000">推销价</th>
							<th style="text-align: center;color: #000">商品规格</th>
							<th style="text-align: center;color: #000">是否特价</th>
							<th style="text-align: center;color: #000">操作</th>
							<th style="text-align: center;color: #000">修改</th>
							<!--  	<th style="text-align: center;color: #000">删除</th>-->
						</tr>
					</table>
					<div class="pageChange" style="margin-bottom:30px">
						<span onclick="prePage()">&nbsp;&nbsp;上一页&nbsp;&nbsp;</span> <span
							onclick="nextPage()">&nbsp;&nbsp;下一页&nbsp;&nbsp;</span> <em>
							&nbsp; 第&nbsp;<span id="pageNum">1</span>&nbsp;页&nbsp;&nbsp;
						</em> <em>/&nbsp;&nbsp;共&nbsp;<span id="totalpage"></span>&nbsp;页
						</em> <em>&nbsp;&nbsp;&nbsp;跳到第<input id="searchInput"
							style="width:46px" maxlength="4" type="text">页
						</em>
						<button class="btn btn-primary" onclick="searchPage()">搜索</button>
					</div>
				</div>
			</div>
		</section>
		<script>
			$(function() {
				selectinfo();
			})
			$(function() {	
			$.post(urls + "superMarketAction/getAllSuperMarketInfo", {
				pageNum: 1,
				pageCount: 2000		
			}, function(res) {
			 	//console.log(res);
				if (res) {
					$.each(res.list, function(n, superInfo) {
					//console.log(superInfo.id);
						$("#directId").append("<option value=" + superInfo.id + ">" + superInfo.name + "</option>");
					})
				}
			})
		});
		
			function selectinfo() {
				var pageNum = $("#pageNum").html();
				var pageCount = $("#pageCount").val();
				var productNames = $("#productNames").val();
			//	var type = $("#types").find("option:selected").val();
				//var directId=$("#directId").find("option:selected").val();
				if (!productNames) {
					productNames = null
				}
				/*if (directId == -1) {
					directId = null
				}
				if (type == -1) {
					type = null;
				}*/
				//console.log(directId)
				$.post(urls + "directAction/getDirectProduct",
					{
						directId : 1,
						pageNum : pageNum,
						pageCount : pageCount,
						productName : productNames,
					//	type : type
					},
					function(data) {
						console.log(data)
						if (data) {
							var totalpage = data.totalpage;
							var orderlist = data.list;
							var table = $("#showtable");
							$("tr").remove(".addtr");
							for (var i = 0; i < orderlist.length; i++) {
								var hots = "";
								var status = "";
								var type = "";
								if (orderlist[i].type == 0) {
									type = "一般商品";
								} else if (orderlist[i].type == 1) {
									type = "特价商品";
								}
								if (orderlist[i].status == 0) {
									status = "未审核";
									hots = "未审核"
								} else if (orderlist[i].status == 1) {
									status = "审核通过";
									if (orderlist[i].putaway == 0) {
										hots = "<button class='btn btn-success' onclick='isPutaway(\"" + orderlist[i].id + "\",1)'>点击上架</button>"
									} else if (orderlist[i].putaway == 1) {
										hots = "<button class='btn btn-primary' onclick='isPutaway(\"" + orderlist[i].id + "\",0)'>点击下架</button>"
									}
								} else if (orderlist[i].status == 2) {
									status = "审核失败";
									hots = "审核失败"
								}
								var append = "<tr id='" + orderlist[i].id + "'  class='addtr'><td style='line-height:120px'>" + (i + 1) + "</td>" +
									"<td ><img style='width:250px;height:120px' src='/JBT/main/" + orderlist[i].adImg + "' alt='加载中'/> </td>" +
									"<td style='vertical-align:middle;'>" + orderlist[i].productName + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].retailPrice + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].hotPrice + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].specification + "</td>" +
									"<td style='vertical-align:middle;'>" + type + "</td>" +
									"<td style='vertical-align:middle;'>" + hots + "</td>" +
									"<td style='line-height:120px'><input onclick='detailinfo(\"" +orderlist[i].id +"\")' type='button' class='btn btn-primary' value='修改' /></td></tr>";
		
								//	"<td style='line-height:120px'><input onclick='deletBurst(\"" + orderlist[i].id + "\")' type='button' class='btn btn-primary' value='删除' /></td>
								table.append(append);
							}
							$("#totalpage").html(totalpage);
						}
					}
					, "json"
				)
			}
			function isPutaway(id, putaway) { //点击上下架商品
				$.post(urls + "directAction/checkDirectProduct",
					{
						id : id,
						putaway : putaway
					},
					function(res) {
						selectinfo()
					}
				);
			}
			function deletBurst(e) { //删除爆款
				if (confirm("是够删除商品？")) {
					$.post(urls + "seckillAction/deleteSeckillOne",
						{
							id : e
						},
						function(res) {
							if (res) {
								$("#" + e).css("display", "none")
							} else {
								alert("删除商品失败")
							}
						})
				}
			}
			function detailinfos() { //添加		
				location.href = "${pageContext.request.contextPath}/YMGback/direct/addDirects.jsp";
			}
			function detailinfo(e) { //点击修改
			location.href = "${pageContext.request.contextPath}/YMGback/direct/changedirect.jsp?id="+e;
			}
		</script>
		
	</div>
	<script>
		var image = '';
		function selectImage(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage2(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image2').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage3(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image3').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		$(function() { //ajax异步提交表单，好东西
			$.post(urls + "seckillAction/getOtherInfo", {
				agentId : localStorage.getItem("agentid")
			}, function(res) {
				if (res) {
					$.each(res, function(n, OtherInfo) {
						$("#selectOther").append("<option value=" + OtherInfo.id + ">" + OtherInfo.otherName + "</option>");
					})
				}
			})
			$("#agentId").val(localStorage.getItem("agentid"))
			$("#setButton").click(function() {
				var reg = /^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
				var config = true;
				if ($("#selectOther").find("option:selected").val() == -1) {
					alert("请选择商家名称")
					return;
				}
				//console.log($("#productName").val().length)
				if ($("#productName").val().length > 30) {
					alert("商品名称长度不能大于30");
					return;
				}
				var index = $("#profit").val().indexOf(".")
				if (index > 0) {
					alert("分销比例不能包含小数点");
					return;
				}
				if ($("#profit").val() > 99 || $("#profit").val() < 1) {
					alert("请输入正确的分销比例");
					return;
				}
				/********************用于判断图片大小以及图片名称是否合法******************/
				var aarr = document.getElementsByTagName("input");
				var barr = [];
				var picreg = /^[A-Za-z0-9\.]{1,100}$/; //图片名称正则
				var flag1 = false; //用于判断图片大小
				var flag2 = false; //用于判断图片名称
				for (var i = 0; i < aarr.length; i++) {
					if (aarr[i].type == "file") {
						barr.push(aarr[i])
					}
				}
				for (var z = 0; z < barr.length; z++) {
					if (barr[z].files.length == 0) {
					} else {
						if (barr[z].files[0].size > 510000) { //判断图片大小是否大于500kb
							flag1 = true;
						}
					/*if (!picreg.test(barr[z].files[0].name)) { 
						flag2 = true;
					}*/
					}
				}
				if (flag1) {
					alert("上传图片不得超过500kb每张");
					return;
				}
				if (flag2) {
					alert("图片名称只能由字母与数字组成");
					return;
				}
				/******************用于判断图片大小以及图片名称是否合法****************/
				if ($("#types1").find("option:selected").val() == -1) {
					alert("请选择商品是否推销");
					return;
				}
				if ($("#classifyId").find("option:selected").val() == 0) {
					alert("请选择商品种类");
					return;
				}
				$.ajax({
					url : urls + "directAction/updateDirectProduct",
					type : 'POST',
					cache : false,
					data : new FormData($('#uploadForm')[0]),
					processData : false,
					contentType : false
				}).done(function(res) {
					if (res == true) {
						alert("提交成功")
						$("#motai").css("display", "none");
						$("#main").css("display", "block");
						selectinfo()
					} else if (res == 251) {
						alert("商品名称已存在");
					} else if (res == false) {
						alert("提交失败")
					}
				}).fail(function(res) {});
			})
	
		})
		
		$(function() {	
			$.post(urls + "superMarketAction/getAllSuperMarketInfo", {
				pageNum: 1,
				pageCount: 2000		
			}, function(res) {
			 	//console.log("jjjjjjjjjjjjjjjjjjj"+res);
				if (res) {
					$.each(res.list, function(n, superInfo) {
					//console.log(superInfo.id);
						$("#selectSuper").append("<option value=" + superInfo.id + ">" + superInfo.name + "</option>");
					})
				}
			})
		});
	</script>
</body>
</html>
